<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-input
        class="search-input"
        placeholder="请输入内容"
        v-model="qureyInfo.query"
      >
        <el-button
          @click="getUsers"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
      <!-- 添加用户按钮 -->
      <el-button type="primary" @click="userInfoDialogVisible = true">添加用户</el-button>
      <!-- 用户table -->
      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" width="180">
        </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <template v-slot:default="slotProps">
            <el-switch v-model="slotProps.row.mg_state"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
            ></el-button>
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="qureyInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 用户表单弹窗 -->
    <el-dialog
      title="添加用户"
      :visible.sync="userInfoDialogVisible"
      width="30%"
      :before-close="handleClose"
      @close="eventClose"
    >
      <!-- 用户表单 -->
      <el-form
        :model="userInfo"
        :rules="userRules"
        ref="userInfo"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userInfo.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="userInfo.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="userInfoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('userInfo')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //用户列表
      userList: [],
      qureyInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      //用户表单
      userInfoDialogVisible: false,
      userInfo: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      userRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password:  [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getUsers() {
      this.$axios
        .get("/users", { params: this.qureyInfo })
        .then((response) => {
          console.log(response);
          const { data: data, status: status } = response;
          if (status !== 200) {
            this.$message.error("获取失败");
            return;
          }
          if (data.meta.status === 200) {
            this.total = data.data.total;
            this.userList = data.data.users;
            console.log();
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    addUsers() {
      this.$axios
        .post("/users", this.userInfo )
        .then((response) => {
          console.log(response);
          const { data: data, status: status} = response;
          if (status !== 200) {
            this.$message.error("提交失败");
            return;
          }
          if (data.meta.status === 201) {
            this.$message.message(data.meta.msg);
          }
          else{
            this.$message.error(data.meta.msg);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    // 分页相关
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      this.qureyInfo.pagesize = val;
      this.getUsers();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.qureyInfo.pagenum = val;
      this.getUsers();
    },
    //table相关
    handleEdit(index, row) {
      //console.log(index, row);
    },
    handleDelete(index, row) {
      //console.log(index, row);
    },
    //对话框相关
    handleClose() {
      this.userInfoDialogVisible = false;
    },
    eventClose(){
      this.$refs.userInfo.resetFields();
    },
    //表单相关
    submitForm(formName) {
        this.userInfoDialogVisible = false;
        console.log(formName);
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addUsers();
            this.getUsers();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
  created() {
    this.getUsers();
  },
};
</script>
<style lang="less" scoped>
.container {
  .box-card {
    margin: 20px 0;
    .search-input {
      width: 480px;
      margin: 0 20px 20px 0;
    }
  }
}
</style>
